package thread.callback.completablefuture;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * @Author: ycy
 * @CreateDate: 2020/7/30 16:24
 */
@Slf4j
public class CompletableFutureDemo {

  public static void main(String[] args) {

    CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
      log.info("完成了");
      int i = 1 / 0;
      try {
        Thread.sleep(3000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return 100;
    });

//    Integer join = future.join();

    log.info("main doing");

    try {
      future.get();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (ExecutionException e) {
      e.printStackTrace();
    }

//    CompletableFuture<Integer> accept = future.whenComplete(new BiConsumer<Integer, Throwable>() {
//      @Override
//      public void accept(Integer integer, Throwable throwable) {
//        log.debug("accept");
//      }
//    });

    future.exceptionally(new Function<Throwable, Integer>() {
      @Override
      public Integer apply(Throwable throwable) {
        throwable.printStackTrace();
        return 1;
      }
    });


  }




}
